package com.doudoukeji.service.Activity.order;

import android.content.Intent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.LinearLayout;

import com.doudoukeji.service.R;
import com.doudoukeji.service.Ui.customlistview.OnRefreshListener;
import com.doudoukeji.service.Ui.customlistview.RefreshListView;
import com.sanhai.android.mvp.EduActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * @version 2.0
 * @类描述： 订单列表
 * @项目名称：ddgh
 * @类名称： OrderListActivity
 * @包名称： com.doudoukeji.service.Activity.order
 * @创建人：zhanjunjie
 * @创建时间：2016/3/9 13:52
 * @修改人：
 * @修改时间：2016/3/9 13:52
 * @修改备注： new parent class(用一句话描述修改了什么)
 */

public class OrderListActivity extends EduActivity implements OrderListView {
    // listview
    private RefreshListView mList;
    private MyOrderAdapter mAdapter;
    private OrderListPresenter presenter;
    private List<OrderListBean> listBeans = new ArrayList<OrderListBean>();

    private String status = "0";
    //页码


    //控件
    private LinearLayout mLlNoOrder;

    private int pageIndex = 1;

    @Override
    public void setContentLayout() {
        setContentView(R.layout.activity_orderlist);
    }

    @Override
    public void init() {
        mLlNoOrder = (LinearLayout) findViewById(R.id.ll_on_order);
        mList = (RefreshListView) findViewById(R.id.list);
        mAdapter = new MyOrderAdapter(this, listBeans);
        mList.setAdapter(mAdapter);
        mList.setHeadAndFoot(true, true);
        mList.setOnRefreshListener(new OnRefreshListener() {

            @Override
            public void onScrollListener(AbsListView view,
                                         int firstVisibleItem, int visibleItemCount,
                                         int totalItemCount) {

            }

            @Override
            public void onPullDownRefresh() {
                pageIndex = 1;
                presenter.getOrderList(pageIndex, status);
            }

            @Override
            public void onLoadMoring() {
                pageIndex++;
                presenter.getOrderList(pageIndex, status);

            }
        });

        mLlNoOrder.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });

    }

    @Override
    public void onClickEvent(View view) {

    }


    @Override
    public void getOrderList(List<OrderListBean> listBeans) {

        if (listBeans != null && listBeans.size() > 0) {
            if (pageIndex == 1) {
                this.listBeans.clear();
            }
            this.listBeans.addAll(listBeans);
            mLlNoOrder.setVisibility(View.GONE);
            mAdapter.notifyDataSetChanged();
        }
        mList.onRefreshFinish();
    }

    @Override
    public void getfail() {
        showToastMessage("请求网路错误或超时");
        mList.onRefreshFinish();
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void getNoData() {
        if (pageIndex == 1) {
            this.listBeans.clear();
            mLlNoOrder.setVisibility(View.VISIBLE);
            mList.onRefreshFinish();
        } else {
            mLlNoOrder.setVisibility(View.GONE);
            mList.showNoMoreData();
        }
        mAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==1002&&resultCode==RESULT_OK){
            pageIndex=1;
            presenter.getOrderList(1,status);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter = new OrderListPresenter(this,this);
        presenter.getOrderList(pageIndex,status);
    }
}
